package com.mdhw.controller;

import com.alibaba.fastjson.JSONObject;
import com.mdhw.common.annotation.IgnorePermissions;
import com.mdhw.common.base.MiliPageData;
import com.mdhw.config.R;
import com.mdhw.guanlin.service.GuanlinService;
import com.mdhw.guanlin.vo.GuanUserVo;
import com.mdhw.guanlin.vo.GuanlinMiliLoginVo;
import com.mdhw.mili.service.MiliService;
import com.mdhw.mili.vo.area.CityVo;
import com.mdhw.mili.vo.area.ParksVo;
import com.mdhw.mili.vo.area.RegionVo;
import com.mdhw.mili.vo.area.VisitorVo;
import com.mdhw.mili.vo.system.DictDataVo;
import com.mdhw.mili.vo.system.FaceCheckVo;
import com.mdhw.mili.vo.user.*;
import com.mdhw.system.service.SysRegionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.ws.rs.POST;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Base64;
import java.util.List;

@Slf4j
@RestController
@Api(tags = "冠林-接口")
@RequestMapping("guanlin")
public class GuanlinController {


    @Autowired
    private MiliService miliService;
    @Autowired
    private GuanlinService guanlinService;
    @Autowired
    private SysRegionService sysRegionService;



    @IgnorePermissions
    @ApiOperation(value = "登录")
    @PostMapping("/post/login")
    public R<GuanUserVo> login(@RequestBody GuanlinMiliLoginVo  guanlinMiliLoginVo){
        log.info("登录接口 {}", JSONObject.toJSONString(guanlinMiliLoginVo));
        String mobile = guanlinMiliLoginVo.getMobile();
        String serialNumber = guanlinMiliLoginVo.getSerialNumber();
        return this.miliService.tokenSocial(guanlinMiliLoginVo);
    }


    @ApiOperation(value = "查询所有小区")
    @GetMapping("/get/selectAllVisitorPage")
    public R<MiliPageData<VisitorVo>> selectAllVisitorPage(
            @RequestParam(value = "current",required = true) Integer current,
            @RequestParam(value = "size",required = true) Integer size,
            @RequestParam(value = "name",required = false) String  name,
            @RequestParam(value = "provinceCode",required = false) String  provinceCode,
            @RequestParam(value = "cityCode",required = false) String  cityCode,
            @RequestParam(value = "countyCode",required = false) String  countyCode,
            @RequestParam(value = "streetCode",required = false) String  streetCode,
            @RequestParam(value = "userId",required = false) Long  userId,
            @RequestParam(value = "type",required = false) String  type,
            @RequestParam(value = "cityName",required = false) String  cityName
    ){

        return  this.guanlinService.selectAllVisitorPage(current, size, name, provinceCode, cityCode, countyCode, streetCode, userId, type, cityName);
    }


    @ApiOperation(value = "查询访客信息")
    @GetMapping("/get/selectFangkeByMobile")
    public R<FangkeVo> selectFangkeByMobile(
            @RequestParam(value = "mobile",required = true) String mobile,
            @RequestParam(value = "projectId",required = true) String projectId
                            ){

        return  this.guanlinService.selectFangkeVoByMobile(mobile, projectId);
    }


    @ApiOperation(value = "查询已开通城市")
    @GetMapping("/get/selectCityList")
    public R<List<CityVo>> selectCityList(
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "provinceCode",required = false) String provinceCode,
            @RequestParam(value = "cityCode",required = false) String cityCode,
            @RequestParam(value = "streetCode",required = false) String streetCode,
            @RequestParam(value = "cityName",required = false) String cityName,
            @RequestParam(value = "projectId",required = false) String projectId
    ){

        List<CityVo> cityVoList = this.guanlinService. selectCityList(name,provinceCode,cityCode,streetCode,cityName, projectId);
        return R.success(cityVoList);
    }


    @ApiOperation(value = "查询业主信息")
    @GetMapping("/get/selectYezhuVo")
    public R<List<YezhuVo>> selectYezhuVo(
            @RequestParam(value = "name",required = true) String name,
            @RequestParam(value = "projectId",required = false) String projectId
    ){

        return this.guanlinService.selectYezhuVo(name, projectId);
    }


    @ApiOperation(value = "查询停车场信息")
    @GetMapping("/get/selectParksList")
    public R<List<ParksVo>> selectParksList(
            @RequestParam(value = "projectId",required = false) String projectId
    ){

        List<ParksVo> parksVoList = this.guanlinService. selectParksList( projectId);
        return R.success(parksVoList);
    }


    @ApiOperation(value = "查询字典")
    @GetMapping("/get/selectMiliDictDataList")
    public R<List<DictDataVo>> selectMiliDictDataList(
            @RequestParam(value = "dictType",required = false) String dictType
    ){

        return this.guanlinService.selectMiliDictDataList( dictType);
    }

    @ApiOperation(value = "访客申请")
    @PostMapping("/post/registerByVisitor")
    public R registerByVisitor (@RequestBody  FangkeRegisterVo fangkeRegisterVo){
        return this.guanlinService.registerByVisitor(fangkeRegisterVo,fangkeRegisterVo.getProjectId());
    }


    @ApiOperation(value = "人脸上传并且校验-使用 base64 编码")
    @PostMapping("/post/faceUploadAndCheckByBase64")
    public R<FaceCheckVo> faceUploadAndCheckByBase64(
        @RequestBody  FaceCheckVo faceCheckVo
    ){
        return this.guanlinService.faceUploadAndCheckByBase64(faceCheckVo.getImageBase64(),faceCheckVo.getProjectId());
    }


    @ApiOperation(value = "人脸上传并且校验")
    @PostMapping("/post/faceUploadAndCheck")
    public R<FaceCheckVo> faceUploadAndCheck(
            @RequestParam(value = "file",required = false) MultipartFile file,
            @RequestParam(value = "projectId",required = false) String projectId

    ){
        return this.guanlinService.faceUploadAndCheck(file,projectId);
    }


    @GetMapping("/get/selectRegionList")
    public R<List<RegionVo>> selectRegionList(){
        R<List<RegionVo>> r = this.sysRegionService.selectRegionTree();
        return r;
    }

    /**
     * 查询访客记录（分页）
     * @return
     */
    @GetMapping("/get/selectFangkeLogPage")
    public R<MiliPageData<FangkeLogVo>> selectFangkeLogPage(
            @RequestParam(value = "current",required = true) Integer current,
            @RequestParam(value = "size",required = true) Integer size,
            @RequestParam(value = "name",required = false) String  name
    ){
        return this.guanlinService.selectFangkeLogPage(current,size,name);
    }

    /**
     * 查询人脸协议
     * @return
     */
    @GetMapping("/get/getAuthorizedFaceFk")
    public R getAuthorizedFaceFk(
            @RequestParam(value = "phone",required = false) String phone,
            @RequestParam(value = "projectId",required = false) String projectId){
        return this.guanlinService.getAuthorizedFaceFk(phone,projectId);
    }


    /**
     * 首页访客通知
     * @return
     */
    @GetMapping("/get/visitorNotification")
    public R visitorNotification(
            @RequestParam(value = "projectId",required = false) String projectId){
        return this.guanlinService.visitorNotification(projectId);
    }


    /**
     * 首页访客记录查询
     * @return
     */
    @GetMapping("/get/selectIndexFangkeLogPage")
    public R<List<IndexFangkeLogVo>> selectIndexFangkeLogPage(
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "projectId",required = false) String projectId
    ){
        return this.guanlinService.selectIndexFangkeLogPage(name,projectId);
    }



    /**
     * 获取访客二维码
     * @return
     */
    @GetMapping("/get/getFangkeQrCode")
    public R<FangkeQrCodeVo> getFangkeQrCode(
            @RequestParam(value = "visitId",required = false) String visitId,
            @RequestParam(value = "projectId",required = false) String projectId
    ){
        return this.guanlinService. getFangkeQrCode(visitId,projectId);
    }

    /**
     * 获取访客二维码
     * @return
     */
    @PostMapping("/post/checkPassRight")

    public R<CheckPassRightVo> checkPassRight(
            @RequestBody  GuanlinMiliLoginVo guanlinMiliLoginVo
    ){
        return this.guanlinService. checkPassRight(guanlinMiliLoginVo.getSerialNumber());
    }


    @GetMapping("/get/selectVisitorLabel")
    public R<List<VisitorReasonVo>> selectVisitorLabel(
            @RequestParam(value = "projectId",required = false) String projectId
    ){
        return this.guanlinService.selectVisitorLabel(projectId);
    }




//    @ApiOperation(value = "查询字典")
//    @GetMapping("/get/visitoraddVisitorByPhone")
//    public R visitoraddVisitorByPhone(
//            @RequestParam(value = "projectId",required = false) String projectId,
//            @RequestParam(value = "projectId",required = false) String projectId,
//    ){
//
//        List<DictDataVo> dictDataVoList = this.guanlinService.  visitoraddVisitorByPhone( dictType);
//        return R.success(dictDataVoList);
//    }
}
